package com.ddx.myopengles20;

import android.opengl.Matrix;

import java.util.Arrays;
import java.util.Stack;

public class MatrixUtils {
    private final float[] vPMatrix = new float[16];//相机投影结果矩阵
    private final float[] projectionMatrix = new float[16];//投影矩阵
    private final float[] viewMatrix = new float[16];//视图矩阵/相机矩阵
    private float[] operateMatrix = {1, 0, 0, 0,
            0, 1, 0, 0,
            0, 0, 1, 0,
            0, 0, 0, 1};//操作矩阵
    private final float[] resultMatrix = new float[16];//结果矩阵

    private final Stack<float[]> stack = new Stack<>();

    public void pushMatrix() {
        stack.push(Arrays.copyOf(operateMatrix, 16));
    }

    public void popMatrix() {
        operateMatrix = stack.pop();
    }

    /**
     * 投影
     */
    public void frustumM(float left, float right, float bottom, float top,
                         float near, float far) {
        Matrix.frustumM(projectionMatrix, 0, left, right, bottom, top, near, far);
    }

    /**
     * 矩阵重置
     */
    public void setIdentityM() {
        Matrix.setIdentityM(operateMatrix, 0);
    }

    /**
     * 设置相机
     */
    public void setLookAtM(float eyeX,
                           float eyeY,
                           float eyeZ,
                           float centerX,
                           float centerY,
                           float centerZ,
                           float upX,
                           float upY,
                           float upZ) {
        Matrix.setLookAtM(viewMatrix, 0, eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);
    }

    /**
     * 平移
     */
    public void translateM(float x, float y, float z) {
        Matrix.translateM(operateMatrix, 0, x, y, z);
    }

    /**
     * 旋转
     * 矩阵的旋转
     * Matrix.rotateM(matrix, offset, a, x, y, z);
     *
     * a:旋转角度
     * 正数：逆时针旋转
     * 负数：顺时针旋转
     *
     * x、y、z：分别表示相应坐标轴 取值0,1类似于Boolen值，例如 x=1代表x轴旋转
     */
    public void rotateM(float angle, float axisX, float axisY, float axisZ) {
        Matrix.rotateM(operateMatrix, 0, angle, axisX, axisY, axisZ);
    }

//    public void rotateM(float angle, float axisX, float axisY, float axisZ){
//        Matrix.rotateM(operateMatrix,0,operateMatrix, 0, angle, axisX, axisY, axisZ);
//    }

    public void setRotateM(float angle, float axisX, float axisY, float axisZ){
        Matrix.setRotateM(operateMatrix, 0, angle, axisX, axisY, axisZ);
    }

    /**
     * 缩放
     */
    public void scaleM(float scaleX, float scaleY, float scaleZ) {
        Matrix.scaleM(operateMatrix, 0, scaleX, scaleY, scaleY);
    }

    public void orthoM(float left, float right, float bottom, float top,
                       float near, float far) {
        Matrix.orthoM(operateMatrix, 0, left, right, bottom, top, near, far);
    }

    public float[] getResultMatrix() {
        Matrix.multiplyMM(vPMatrix, 0, projectionMatrix, 0, viewMatrix, 0);
        Matrix.multiplyMM(resultMatrix, 0, vPMatrix, 0, operateMatrix, 0);
        return resultMatrix;
    }
}


